<template>
  <div class="login" @click="selectShow = false">
    <div class="balanceBox" v-if="isLogin">
      <h1>{{ $t("views.index.login.balance.title") }}</h1>

      <div class="cashBox">
        <img src="@/assets/image/indexImage/bitcoin.png" alt="" />
        <p>{{ balance }}</p>
        <!-- <span>币</span> -->
      </div>
    </div>
    <div class="loginBox" v-else>
      <h1>
        {{ $t("views.index.login.title") }}
      </h1>

      <div class="fontBox">
        <div class="selectInput">
          <div
            class="selectBox"
            @mouseenter="selectShow = true"
            @mouseleave="selectShow = false"
          >
            <div class="select" v-show="selectShow"  @click.stop>
              <ul class="selectContent" @touchmove.stop>
                <li
                  :class="{
                    selectItem: true,
                    change: item.code == countryCode,
                  }"
                  v-for="(item, index) in interCodeList"
                  @click="()=>{countryCode = item.code,selectShow = false}"
                  :key="index"
                >
                  <span>+</span>
                  <span>{{ item.code }}</span>
                </li>
              </ul>
            </div>

            <div class="selectChangeBox" @click="selectShow = !selectShow" @click.stop>
              <div class="changeText">
                <span>+</span>
                <span>{{ countryCode }}</span>
              </div>
              <van-icon
                :class="{ icon: true, change: selectShow }"
                name="arrow-down"
              />
            </div>
          </div>

          <input
            class="loginName"
            type="text"
            :placeholder="$t('views.index.login.from.loginName.placeholder')"
            v-model="userName"
          />
        </div>
        <input
          class="loginPassword"
          type="password"
          :placeholder="$t('views.index.login.from.loginPassword.placeholder')"
          v-model="userPassword"
        />
        <button @click="loginChange">
          {{ $t("views.index.login.from.button.title") }}
        </button>
      </div>
    </div>

    <footer>
      <div class="hrBox">
        <hr />
        <span>MarsCoin</span>
        <hr />
      </div>

      <div class="footerContent">
        <div class="functionBox">
          <div
            class="functionItem"
            @click="
              goRouter(
                'whatsapp://send/?phone=15709141446&text=Hi&type=phone_number&app_absent=0',
                true
              )
            "
          >
            <img class="image" src="@/assets/image/indexImage/news.png" />
          </div>
          <div
            class="functionItem"
            @click="goRouter('tg://resolve?domain=MarsCoin_CS', true)"
          >
            <img class="image" src="@/assets/image/indexImage/telegram.png" />
          </div>
        </div>
      </div>

      <p class="bottonNote filingText">
        © 2013-2024 | The MarsBox Foundation & MarsBox Project. All rights
        reserved
      </p>
      <p class="bottonNote emailText">
        {{ $t("views.index.email") }}：MarsboxVIP@gmail.com
      </p>
    </footer>
  </div>
</template>
<script>
import internationalCode from "@/config/internationalCode";
import { Icon } from "vant";
import Vue from "vue";
Vue.use(Icon);
import { cipherLogin, getMemberInfo } from "@/api/api";
export default {
  data() {
    return {
      countryCode: 86,
      userName: "",
      userPassword: "",
      balance: "0.00",
      interCodeList: [],
      selectShow: false,
    };
  },
  computed: {
    isLogin() {
      return this.$store.state.token ? true : false;
    },
  },
  mounted() {
    this.interCodeList = internationalCode;
    this.countryCode = internationalCode[0].code;
    if (this.$store.state.token) {
      getMemberInfo().then((res) => {
        if (res.code == 10000) {
          this.$store.commit("setToken", { token: "" });
          this.$router.go();
          return false;
        }
        if (res.code == 0) {
          this.balance = res.data.mars_coin;
        } else {
          alert(res.msg);
        }
      });
    }
  },
  methods: {
    loginChange() {
      const userNameValidate = /^\d{8,11}$/;
      if (!userNameValidate.test(this.userName)) {
        return alert(this.$t("views.index.login.tip.loginName.verification"));
      }
      const userPasswordValidate = /^.{8,20}$/;
      if (!userPasswordValidate.test(this.userPassword)) {
        return alert(
          this.$t("views.index.login.tip.loginPassword.verification")
        );
      }
      cipherLogin({
        account: this.userName,
        password: this.userPassword,
        country_code: this.countryCode,
      }).then((res) => {
        if (res.code == 0) {
          this.$store.commit("setToken", { token: res.data.token });
          this.balance = res.data.mars_coin;
          alert(this.$t("views.index.login.tip.signIn.success"));
        } else {
          alert(res.msg);
        }
      });
    },
    goRouter(routeUrl, external) {
      if (external) {
        window.open(routeUrl);
      } else {
        this.$router.push({ path: routeUrl });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url("@/assets/image/indexBgImage/indexBgImage_login.jpg")
    no-repeat;
  background-size: cover;
  overflow-y: scroll;
  box-sizing: border-box;
  padding: 125px 36px 26px;
  display: flex;
  flex-direction: column;
}

.loginBox {
  width: 100%;
  flex: 1;

  h1 {
    //font-family: HelloFont;
    font-weight: bold;
    font-size: 60px;
    color: #ffffff;
    line-height: 58px;
    margin-top: 244px;
  }

  .fontBox {
    margin-top: 50px;
    display: flex;
    flex-direction: column;

    .selectInput {
      width: 600px;
      height: 80px;
      border-radius: 4px;
      border: 1px solid #ffffff;
      display: flex;
      box-sizing: border-box;

      .selectBox {
        width: 135px;
        height: 100%;
        position: relative;

        .select {
          width: 130px;
          height: 190px;
          position: absolute;
          top: 100%;
          right: 0;
          background: #0e0e0e;
          border-radius: 8px;
          box-sizing: border-box;
          padding: 20px 0;
          z-index: 1;

          .selectContent {
            width: 100%;
            height: 100%;
            overflow: scroll;
            list-style: none;
            // font-family: Source Han Sans CN;
            font-weight: bold;
            font-size: 18px;
            color: #ffffff;

            .selectItem {
              padding: 5px 35px;
              box-sizing: border-box;

              &.change {
                background: #1e1e1e;
              }

              &:hover {
                background: #1e1e1e;
              }
            }

            &::-webkit-scrollbar {
              display: none;
            }
          }
        }

        &::after {
          content: "";
          width: 1px;
          height: 34px;
          display: block;
          background: #ffffff;
          position: absolute;
          top: 50%;
          right: 0;
          transform: translateY(-50%);
        }

        .selectChangeBox {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          justify-content: space-between;
          box-sizing: border-box;
          padding: 0 20px 0 36px;

          .changeText {
            // font-family: Source Han Sans CN;
            font-weight: bold;
            font-size: 18px;
            color: #ffffff;
          }

          .icon {
            font-size: 14px;
            font-weight: bold;
            color: #ffffff;

            &.change {
              transform: rotateZ(180deg);
            }
          }
        }
      }

      .loginName {
        flex: 1;
        height: 100%;
        border: none;
        box-sizing: border-box;
        padding: 0 20px;
        outline: none;
        color: #ffffff;
        background: none;
        // font-family: Source Han Sans CN;
        font-weight: bold;
        font-size: 18px;

        &::placeholder {
          color: #ffffff;
          opacity: 0.3;
        }
      }
    }

    .loginPassword {
      width: 600px;
      height: 80px;
      border-radius: 4px;
      border: 1px solid #ffffff;
      box-sizing: border-box;
      padding: 0 20px;
      outline: none;
      color: #ffffff;
      background: none;
      // font-family: Source Han Sans CN;
      font-weight: bold;
      font-size: 18px;
      margin-top: 15px;

      &::placeholder {
        color: #ffffff;
        opacity: 0.3;
      }
    }

    button {
      width: 600px;
      height: 64px;
      margin-top: 50px;
      background: #ffffff;
      border-radius: 4px;
      border: none;
      // font-family: Source Han Sans CN;
      font-weight: bold;
      font-size: 24px;
      color: #333333;
    }
  }
}

.balanceBox {
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;

  h1 {
    //font-family: HelloFont;
    font-weight: bold;
    font-size: 36px;
    color: #ffffff;
    line-height: 34px;
  }

  .cashBox {
    display: flex;
    align-items: end;
    color: #ffffff;
    margin-top: 50px;

    img {
      width: 86px;
      height: 86px;
      display: inline-block;
    }

    p {
      //font-family: Source Han Sans CN;
      font-weight: bold;
      font-size: 102px;
      line-height: 90px;
      margin-left: 34px;
      margin-right: 28px;
    }

    span {
      //font-family: HelloFont;
      font-weight: normal;
      font-size: 36px;
      line-height: 32px;
    }
  }
}

footer {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;

  .hrBox {
    width: 100%;
    display: flex;
    align-items: center;

    hr {
      width: 100%;
      height: 1px;
      background: #ffffff;
      opacity: 0.3;
      border: none;
    }

    span {
      //font-family: Arial;
      font-weight: bold;
      font-size: 48px;
      color: #ffffff;
      line-height: 36px;
      margin: 0 30px;
    }
  }

  .footerContent {
    box-sizing: border-box;
    margin-top: 30px;
    display: flex;
    justify-content: space-between;

    .functionBox {
      width: 270px;
      display: flex;
      justify-content: space-around;

      .functionItem {
        width: 122px;
        height: 122px;
        box-sizing: border-box;
        border: 8px solid #ffffff;
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;

        .image {
          height: 40px;
        }
      }
    }
  }

  .bottonNote {
    //font-family: Microsoft YaHei;
    width: 534px;
    font-weight: 400;
    font-size: 18px;
    color: #ffffff;
    line-height: 26px;

    &.filingText {
      margin-top: 32px;
      text-align: center;
    }

    &.emailText {
      text-align: center;
    }
  }
}
</style>
